package com.itheima.controller;

import com.itheima.domain.annotation.LogOperation;
import com.itheima.domain.pojo.Dept;
import com.itheima.domain.vo.Result;
import com.itheima.service.DeptService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Sch
 * @Date: 2025/07/10 10:25
 * @Description:
 */

/**
 * 部门管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 查询部门列表
     */
    @GetMapping
    public Result findAll() {
        List<Dept> deptList = deptService.findAll();
        log.info("查询部门列表");
        return Result.success(deptList);
    }

    /**
     * 删除部门
     */
    @LogOperation
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        deptService.deleteById(id);
        //System.out.println("根据ID删除部门" + id);
        log.info("根据ID删除部门");
        return Result.success();
    }

    /**
     * 新增部门
     */
    @LogOperation
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        //System.out.println("新增部门,dept=" + dept);
        log.info("新增部门");
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        //System.out.println("根据ID查询,id="+id);
        log.info("根据ID查询");
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @LogOperation
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        //System.out.println("修改部门,dept=" + dept);
        log.info("修改部门");
        deptService.update(dept);
        return Result.success();
    }
}
